﻿using GameEntitySystem;
using Game;
using TemplatesDatabase;
using Engine.Graphics;
using Engine;
using Mekiasm.Buff;

namespace Mekiasm.ExtraGui
{
    public class ComponentExtraGui : Component,IUpdateable
    {
        public ComponentGui Gui;
        //扳手的额外按钮
        public ImageWidget ExtraSet = new ImageWidget() { Name = "ExtraSet" };
        public StackPanelWidget Container = new StackPanelWidget() { Direction = LayoutDirection.Vertical, HorizontalAlignment = WidgetAlignment.Far, VerticalAlignment = WidgetAlignment.Center };
        //人物装备栏更改
        public MekShortInventoryWidget mekShortInventoryWidget = new MekShortInventoryWidget() { IsVisible = true, IsDrawRequired = true };
        public StackPanelWidget BuffView = new StackPanelWidget() { Name = "MekBuffView", Direction = LayoutDirection.Horizontal, HorizontalAlignment = WidgetAlignment.Center };
        public ComponentBuff ComponentBuff;


        public UpdateOrder UpdateOrder => UpdateOrder.Default;
        public override void OnEntityAdded()
        {
            Gui.ControlsContainerWidget.Children.Add(BuffView);
        }

        public override void OnEntityRemoved()
        {
            BuffView.Children.Clear();
            Gui.ControlsContainerWidget.Children.Remove(BuffView);
            ShortInventoryWidget shortInventoryWidget = Gui.ControlsContainerWidget.Children.Find<ShortInventoryWidget>();
            StackPanelWidget ShortInventoryPanel = shortInventoryWidget.ParentWidget as StackPanelWidget;
            ShortInventoryPanel.Children.Remove(mekShortInventoryWidget);
            Gui.ControlsContainerWidget.Children.Remove(Container);
        }
        public void AddBuff(int id, double buffTime)
        {
            ComponentBuff?.AddBuff(id, buffTime);
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            base.Load(valuesDictionary, idToEntityMap);
            Gui = Entity.FindComponent<ComponentGui>();
            ComponentBuff = Entity.FindComponent<ComponentBuff>();
            Gui.ControlsContainerWidget.Children.Add(Container);
            ShortInventoryWidget shortInventoryWidget = Gui.ControlsContainerWidget.Children.Find<ShortInventoryWidget>();
            shortInventoryWidget.IsVisible = false;
            mekShortInventoryWidget.AssignComponents(Gui.m_componentPlayer.ComponentMiner.Inventory);
            StackPanelWidget ShortInventoryPanel = shortInventoryWidget.ParentWidget as StackPanelWidget;
            ShortInventoryPanel.Children.Add(mekShortInventoryWidget);
            ExtraSet.Texture = ContentManager.Get<Texture2D>("Mekiasm/Gui/Overlay/wrench");
            ExtraSet.Size = new Vector2(48f);
            ExtraSet.HorizontalAlignment = WidgetAlignment.Far;
            ExtraSet.VerticalAlignment = WidgetAlignment.Center;
            Container.Children.Add(ExtraSet);
        }
        private int? LastBlockValue;
        public void Update(float dt)
        {
            var m_componentPlayer = Gui.m_componentPlayer;
            int v = m_componentPlayer.ComponentMiner.ActiveBlockValue;
            if (LastBlockValue.HasValue && LastBlockValue.Value != v)
            {
                int Id2 = Terrain.ExtractContents(LastBlockValue.Value);
                if (MekiasmInit.MekBlocks.TryGetValue(Id2, out var mek2)) mek2.OnHandOut(m_componentPlayer, LastBlockValue.Value, v);
            }
            int Id = Terrain.ExtractContents(v);
            if (MekiasmInit.MekBlocks.TryGetValue(Id, out MekBlock mekBlock))
            {
                if (ExtraSet.IsVisible = mekBlock.HasExtraSet(v))
                {
                    WidgetInput widgetInput = m_componentPlayer.GameWidget.Input;
                    if ((widgetInput.IsKeyDown(Engine.Input.Key.Control) && widgetInput.IsKeyDownOnce(Engine.Input.Key.M)) || (widgetInput.Click.HasValue && ExtraSet.HitTest(widgetInput.Click.Value.Start) && ExtraSet.HitTest(widgetInput.Click.Value.End)))
                    {
                        mekBlock.ClickExtraSet(m_componentPlayer, v);
                    }
                }
                mekBlock.OnHand(m_componentPlayer, v);
                if (m_componentPlayer.ComponentInput.PlayerInput.Aim.HasValue)
                {
                    mekBlock.OnAim(v, m_componentPlayer.ComponentInput.PlayerInput.Aim.Value, m_componentPlayer.ComponentMiner, AimState.Completed);
                }
            }
            else
            {
                ExtraSet.IsVisible = false;
            }
            LastBlockValue = v;
        }
    }
}
